<?php defined('SYSPATH') OR die('No direct access allowed.');

class Contact_Controller extends Website_Controller {

	protected function _content($args)
	{
		$this->session = $sess = Session::instance();

		$errorFill = 'Fill in your name, email, and message.';

		$in = $this->input;
		$name	= $in->post('name');
		$email	= $in->post('email');
		$text	= $in->post('text');

		if ('send' == $in->post('submit')) {
			if (!$name || !$email || !$text) {
				$sess->set('errorMsg',$errorFill);
			} else {
				$human   = $in->post('human');
				$human_x = $sess->get('human_x');
				if ($human and ($human==$human_x)) {
					// submitted and verified - mail
					$to		= 'igb@jbnotes.info';
					$subj	= '[[jBnotes]] Contact';
					$msg	= $text."\n-----\nname: $name\nemail: $email";

					if (mail::sendMail("$name <$email>",$to,$subj,$msg)) {
						$sess->set('infoMsg','Thank you, your message has been sent.');
						$name = $email = $text = '';
					} else {
						$sess->set('errorMsg','The post office was closed, try another time.');
					}
				}
			}
		}

		$human_a = rand(1,9);
		$human_b = rand(1,9);
		$human_x = $human_a + $human_b;

		$sess->set('human_x',$human_x);

		$name  = html::hQ($name);
		$email = html::hQ($email);
		$text  = html::hQ($text,'');

		return <<<HEREDOC
<script type='text/javascript'><!--
function on_submit()
{
	if (!frm.name.value || !frm.email.value || !frm.text.value) {
		setErrorMsg('$errorFill');
		return false;
	}

	if (frm.human.value!=$human_x) {
		setErrorMsg('Answer the question, please');
		return false;
	}

	return true;
}
//--></script>
<form name='form' action='' method='post' onsubmit='return on_submit()'>
<div class="frame">
	<table class="panel"  summary="">
	<tr><th></th><th>Send me a message:</th></tr>
	<tr>
		<td class="tag">Your&nbsp;name:</td>
		<td><input type='text' name='name' value=$name size="22"/></td>
	</tr><tr>
		<td class="tag">Your&nbsp;email:</td>
		<td><input type='text' name='email' value=$email size="22"/></td>
	</tr><tr>
		<td class="tag">Message:</td>
		<td><textarea name='text' cols='48' rows='6'>$text</textarea></td>
	</tr><tr>
		<td class="tag"></td>
		<td>(human test) Calculate: $human_a+$human_b = <input type='text' name='human' value="" size="3"/></td>
	</tr><tr>
		<td class="tag"></td>
		<td><input type='submit' name='submit' value='send'/></td>
	</tr>
	</table>
</div>
</form>
<script type='text/javascript'><!--
var frm = document.form;
//--></script>
HEREDOC;

	}
}

// eof